package com.sparrow.common.mergeupdate.impl;

import com.sparrow.common.thread.ThreadPool;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class SimpleDelayMergerBox {

    private AtomicBoolean changeStatus = new AtomicBoolean(false);

    public void tryMergeSchedule(Runnable r, long delay, TimeUnit unit) {
        if (!changeStatus.compareAndSet(false, true)) {
            return; // 已调度，不重复调度
        }

        ThreadPool.SCHEDULER.schedule( () -> {
            if (changeStatus.compareAndSet(true, false)) {
                r.run();
            }
        }, delay, unit);
    }
}
